import axios from "axios";
import ApiConfig from "@/config/ApiConfig";
import { ElMessage } from "element-plus";

// enum MSGS {
//   "操作成功" = 200,
//   "密码错误" = 201,
//   "账号错误",
//   "请求异常",
// }

const $http = axios.create({
  baseURL: `${ApiConfig.baseurl}`,
  timeout: 2000,
  headers: {
    "Content-Type": "application/json;charset=utf-8",
  },
});

// 请求拦截

$http.interceptors.request.use((config) => {
  config.headers = config.headers || {};
  const token = localStorage.getItem("token");
  if (token) {
    config.headers.token = localStorage.getItem("token") || "";
  }
  return config;
});

// 返回拦截
$http.interceptors.response.use(
  (res) => {
    const code: number = res.data.code;
    if (code != 0) {
      ElMessage.error("Oops, this is a error message.");
      return Promise.reject(res.data);
    }
    return res.data;
  },

  (err) => {
    console.log(err);
  }
);

export default $http;
